package net.lzzy.Job2;

import org.apache.spark.SparkConf;
import org.apache.spark.api.java.JavaPairRDD;
import org.apache.spark.api.java.JavaRDD;
import org.apache.spark.api.java.JavaSparkContext;
import org.apache.spark.api.java.function.PairFunction;
import org.apache.spark.api.java.function.VoidFunction;
import scala.Tuple2;

public class SparkJob_Demo2 {
    public static void main(String[] args) {
        SparkConf conf = new SparkConf().setAppName("SparkTextFile").setMaster("local");
        JavaSparkContext javaSparkContext = new JavaSparkContext(conf);
        JavaRDD<String> javaRDD = javaSparkContext.textFile("D:\\Spark\\trip.csv");
        JavaPairRDD<SortBy, String> sortByStringJavaPairRDD = javaRDD.mapToPair(new PairFunction<String, SortBy, String>() {
            @Override
            public Tuple2<SortBy, String> call(String s) throws Exception {
                String[] sortByKey = s.split(",");
                return new Tuple2<SortBy, String>(new SortBy(Integer.parseInt(sortByKey[3]),
                        Integer.parseInt(sortByKey[4])), s);
            }
        }).sortByKey(true);
        sortByStringJavaPairRDD.foreach(new VoidFunction<Tuple2<SortBy, String>>() {
            @Override
            public void call(Tuple2<SortBy, String> sortByStringTuple2) throws Exception {
                System.out.println(sortByStringTuple2._2);
            }
        });
    }
}
